package com.l.web.master.system.role;

import org.apache.log4j.Logger;
import org.l.qiao.ClusterUtil;
import org.l.qiao.Form;
import org.l.qiao.LConstant;
import org.l.qiao.Validate;
import org.l.qiao.annotation.Privilege;
import org.l.qiao.web.ControllerSupport;
import org.l.qiao.web.KeyUpdate;
import org.l.qiao.web.ResponseCode;
import org.l.qiao.web.ResponseInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping(RoleController.URI)
public class RoleController extends ControllerSupport {
    public static final String URI = LConstant.MASTER_URI + "/system/role";
    private static final Logger LOGGER = Logger.getLogger(RoleService.class);
    @Autowired
    private RoleService roleService;

    @Privilege(module = "角色", function = "角色管理", operation = "查询")
    @RequestMapping("index.do")
    public ModelAndView index(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
        modelMap.put(LConstant.CONTROLLER_KEY, URI);
        return new ModelAndView(URI + "/index", modelMap);
    }

    @Privilege(module = "角色", function = "角色管理", operation = "查询")
    @RequestMapping("tree.do")
    public ModelAndView list(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
        List roles = roleService.getList();
        modelMap.put("roles", roles);
        return new ModelAndView(URI + "/tree", modelMap);
    }

    @Privilege(module = "角色", function = "角色管理", operation = "详情")
    @RequestMapping("detail.do")
    public ModelAndView detail(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws IOException {
        Form form = getForm(request);
        String id = form.getString("id");
        LOGGER.debug("查看角色信息：id=" + id);
        if (Validate.check(Validate.OBJECTID, id)) {
            boolean result = roleService.detail(id, modelMap);

            if (!result) {
                LOGGER.error("所查看的角色不存在");
                response.sendError(404);
                return null;
            }
        } else {
            LOGGER.error("非法查看角色信息：id=" + id);
            response.sendError(404);
            return null;
        }

        return new ModelAndView(URI + "/detail", modelMap);
    }

    @Privilege(module = "角色", function = "角色管理", operation = "添加")
    @RequestMapping("add.do")
    public ModelAndView add(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
        List roles = roleService.getList();
        modelMap.put("parentId", request.getParameter("parentId"));
        modelMap.put("roles", roles);
        modelMap.put("check", ClusterUtil.getCheck(Role.class));
        return new ModelAndView(URI + "/add", modelMap);
    }

    @Privilege(module = "角色", function = "角色管理", operation = "添加")
    @RequestMapping("save.do")
    public void save(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws IOException {
        Form form = getForm(request);
        Role role = new Role();
        boolean result = form.getCluster(role);
        LOGGER.debug("添加角色：" + role);

        if (result) {
            ResponseInfo responseInfo = roleService.save(role);
            out(response, responseInfo);
        } else {
            response.sendError(404);
            LOGGER.error("非法添加角色：" + role);
        }
    }


    @Privilege(module = "角色", function = "角色管理", operation = "修改")
    @RequestMapping("update.do")
    public void update(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws IOException {
        Form form = getForm(request);
        KeyUpdate keyUpdate = form.getKeyUpdate(Role.class);
        LOGGER.debug("修改信息信息：" + keyUpdate);
        if (keyUpdate.check()) {
            int code = ResponseCode.FAILURE;

            try {
                code = roleService.update(keyUpdate);
            } catch (Exception e) {
                LOGGER.error("修改异常[" + keyUpdate + "]：" + e);
            }

            out(response, code);
        } else {
            LOGGER.error("非法修改：" + keyUpdate);
            response.sendError(404);
        }
    }

    @Privilege(module = "角色", function = "角色管理", operation = "修改")
    @RequestMapping("parent.do")
    public ModelAndView parent(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws IOException {
        String id = request.getParameter("id");
        boolean result = Validate.check(Validate.OBJECTID, id);
        if (result) {
            List roles = roleService.parent(id);
            modelMap.put("roles", roles);
        } else {
            LOGGER.error("非法查询角色树：id=" + id);
            response.sendError(404);
            return null;
        }
        return new ModelAndView(URI + "/parent", modelMap);
    }

    @Privilege(module = "角色", function = "角色管理", operation = "修改")
    @RequestMapping("limit.do")
    public ModelAndView limit(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws IOException {
        List list = roleService.limit();
        modelMap.put("limitConfig", list);
        return new ModelAndView(URI + "/limit", modelMap);
    }

    @Privilege(module = "角色", function = "角色管理", operation = "删除")
    @RequestMapping("remove.do")
    public void remove(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws IOException {
        String id = request.getParameter("id");
        LOGGER.debug("删除菜单：id=" + id);

        if (Validate.check(Validate.OBJECTID, id)) {
            int code = roleService.remove(id);
            out(response, code);
        } else {
            LOGGER.error("非法删除菜单：id=" + id);
            response.sendError(404);
        }
    }
}
